<template>
    <el-dialog
            :title="!dataForm.id ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
                 label-width="80px">
            <el-form-item label="用户ID，保证唯一" prop="uid">
                <el-input v-model="dataForm.uid" placeholder="用户ID，保证唯一"></el-input>
            </el-form-item>
            <el-form-item label="年龄段" prop="ageGroup">
                <el-input v-model="dataForm.ageGroup" placeholder="年龄段"></el-input>
            </el-form-item>
            <el-form-item label="目的" prop="purpose">
                <el-input v-model="dataForm.purpose" placeholder="目的"></el-input>
            </el-form-item>
            <el-form-item label="是否领取" prop="isGet">
                <el-input v-model="dataForm.isGet" placeholder="是否领取"></el-input>
            </el-form-item>
            <el-form-item label="创建时间" prop="createTime">
                <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
            </el-form-item>
            <el-form-item label="等级" prop="level">
                <el-input v-model="dataForm.level" placeholder="等级"></el-input>
            </el-form-item>
            <el-form-item label="学习进度" prop="studySityation">
                <el-input v-model="dataForm.studySityation" placeholder="学习进度"></el-input>
            </el-form-item>
            <el-form-item label="更新时间" prop="phone">
                <el-input v-model="dataForm.phone" placeholder="更新时间"></el-input>
            </el-form-item>
            <el-form-item label="来源" prop="clientType">
                <el-input v-model="dataForm.clientType" placeholder="来源"></el-input>
            </el-form-item>
            <el-form-item label="是否领取" prop="isClick">
                <el-input v-model="dataForm.isClick" placeholder="是否领取"></el-input>
            </el-form-item>
            <el-form-item label="单词" prop="word">
                <el-input v-model="dataForm.word" placeholder="单词"></el-input>
            </el-form-item>
            <el-form-item label="更新时间" prop="updateTime">
                <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        data() {
            return {
                visible: false,
                dataForm: {
                    id: 0,
                    uid: '',
                    ageGroup: '',
                    purpose: '',
                    isGet: '',
                    createTime: '',
                    level: '',
                    studySityation: '',
                    phone: '',
                    clientType: '',
                    isClick: '',
                    word: '',
                    updateTime: ''
                },
                dataRule: {
                    uid: [
                        {required: true, message: '用户ID，保证唯一不能为空', trigger: 'blur'}
                    ],
                    ageGroup: [
                        {required: true, message: '年龄段不能为空', trigger: 'blur'}
                    ],
                    purpose: [
                        {required: true, message: '目的不能为空', trigger: 'blur'}
                    ],
                    isGet: [
                        {required: true, message: '是否领取不能为空', trigger: 'blur'}
                    ],
                    createTime: [
                        {required: true, message: '创建时间不能为空', trigger: 'blur'}
                    ],
                    level: [
                        {required: true, message: '等级不能为空', trigger: 'blur'}
                    ],
                    studySityation: [
                        {required: true, message: '学习进度不能为空', trigger: 'blur'}
                    ],
                    phone: [
                        {required: true, message: '更新时间不能为空', trigger: 'blur'}
                    ],
                    clientType: [
                        {required: true, message: '来源不能为空', trigger: 'blur'}
                    ],
                    isClick: [
                        {required: true, message: '是否领取不能为空', trigger: 'blur'}
                    ],
                    word: [
                        {required: true, message: '单词不能为空', trigger: 'blur'}
                    ],
                    updateTime: [
                        {required: true, message: '更新时间不能为空', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.id = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.id) {
                        this.$http({
                            url: this.$http.adornUrl(`/generator/eduuserextend/info/${this.dataForm.id}`),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataForm.uid = data.eduUserExtend.uid
                                this.dataForm.ageGroup = data.eduUserExtend.ageGroup
                                this.dataForm.purpose = data.eduUserExtend.purpose
                                this.dataForm.isGet = data.eduUserExtend.isGet
                                this.dataForm.createTime = data.eduUserExtend.createTime
                                this.dataForm.level = data.eduUserExtend.level
                                this.dataForm.studySityation = data.eduUserExtend.studySityation
                                this.dataForm.phone = data.eduUserExtend.phone
                                this.dataForm.clientType = data.eduUserExtend.clientType
                                this.dataForm.isClick = data.eduUserExtend.isClick
                                this.dataForm.word = data.eduUserExtend.word
                                this.dataForm.updateTime = data.eduUserExtend.updateTime
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/generator/eduuserextend/${!this.dataForm.id ? 'save' : 'update'}`),
                            method: 'post',
                            data: this.$http.adornData({
                                'id': this.dataForm.id || undefined,
                                'uid': this.dataForm.uid,
                                'ageGroup': this.dataForm.ageGroup,
                                'purpose': this.dataForm.purpose,
                                'isGet': this.dataForm.isGet,
                                'createTime': this.dataForm.createTime,
                                'level': this.dataForm.level,
                                'studySityation': this.dataForm.studySityation,
                                'phone': this.dataForm.phone,
                                'clientType': this.dataForm.clientType,
                                'isClick': this.dataForm.isClick,
                                'word': this.dataForm.word,
                                'updateTime': this.dataForm.updateTime
                            })
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>
